# coding=utf-8
# 部门模块
import random

from inter_auto.script.wework import WeWork


class Department(WeWork):


    def __init__(self):
        super().__init__()
        self.token = self.get_token()
        self.department_list_url = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist"
        self.create_department_url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={self.token}"
        self.delete_department_url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete"

    def departemn_list(self):
        """
        获取部门id列表
        :param url:
        :param params:
        :return:
        """
        data = {
            "access_token": self.token,
            "id": 1
        }
        res = self.get(self.department_list_url, params=data).json()
        subdivision_id = [i["id"] for i in res.get("department_id")]
        return subdivision_id


    def create_department(self,new_sub_id):
        """
        新增部门
        :param new_sub_id:
        :return:
        """
        self.clear_environment()
        data = {
            "name": f"广州研发中心{new_sub_id}",
            "name_en": f"RDGZ1{new_sub_id}",
            "parentid": 1,
            "order": new_sub_id,
            "id": new_sub_id
        }
        res = self.post(self.create_department_url,params=data).json()
        return res


    def delete_department(self,department_id):
        """
        删除部门
        :param department_id:
        :return:
        """
        data = {
            "access_token": self.token,
            "id": department_id
        }
        res = self.get(self.delete_department_url,params=data).json()
        return res


    def clear_environment(self):
        """
        除了父部门，删除所有子部门
        :return:
        """
        departemn_list = self.departemn_list()
        for departemn_id in departemn_list:
            if departemn_id != 1:
                self.delete_department(departemn_id)












